import {baseUrl}  from './config'
import {request,RequestConfig,HttpRequestMethod,LengyuHttpResponse,upload,PageRequestParam}  from '../utils/request'

export interface WorkOrder{
  id?:string,
  createTime?:string,
  updateTime?:string,
  title?:string,
  userId?:string,
  vxOpenId?:string,
  orderType?:string,
  deviceType?:string,
  priority?:string,
  deadlineDate?:Date,
  deadlineTime?:Date,
  imgUrlList?:string,
  procUserIds?:string,
  instruction?:string,
  currentStatus?:string, 
}

export interface WorkOrderUser{
  id?:string,
  createTime?:string,
  updateTime?:string,
  workOrderId?:string,
  userId?:string,
  userVxOpenId?:string,
  position?:string,
  career?:string,
}

// 将微信用户信息保存到数据库
export function save(workOrder:WorkOrder,workOrderUsers:Array<any>): Promise<LengyuHttpResponse> {
  workOrder.vxOpenId=wx.getStorageSync('openId')
  workOrder.userId=wx.getStorageSync('userId')
  
  let workOrderUserList:Array<WorkOrderUser> = []
  let userIdsStr = "";
  if(workOrderUsers){
    workOrderUsers.forEach((item,index)=>{
      let user:WorkOrderUser = {}
      user.userVxOpenId=item.vxOpenId;
      user.userId=item.id
      if(workOrder.id){
        user.workOrderId=workOrder.id
      }
      workOrderUserList.push(user);
      if(index == 0){
        userIdsStr += item.id;
      }
      else{
        userIdsStr += ",";
        userIdsStr += item.id;
      }
    })
  }
  workOrder.procUserIds=userIdsStr;
  let data={
    workOrder:workOrder,
    workOrderUserList:workOrderUserList
  }
  let requestConfig:RequestConfig={
    url:'/work-order/save',
    method:HttpRequestMethod.POST,
    data:data,
    token:true,
  }
  return request(requestConfig);
}

export function page(param:PageRequestParam): Promise<LengyuHttpResponse> {
  console.log("工单分页查询：", param)
  let requestConfig:RequestConfig={
    url:'/work-order/page',
    method:HttpRequestMethod.POST,
    data:param,
    token:true,
  }
  return request(requestConfig);
}

export function detail(id:string): Promise<LengyuHttpResponse> {
  console.log("工单详细信息查询：", id)
  let requestConfig:RequestConfig={
    url:'/work-order/detail?id='+id,
    method:HttpRequestMethod.GET,
    token:true,
  }
  return request(requestConfig);
}


export function getChartData(): Promise<LengyuHttpResponse> {
  let requestConfig:RequestConfig={
    url:'/work-order/getChartData',
    method:HttpRequestMethod.POST,
    token:true,
  }
  return request(requestConfig);
}

// export  function downloadAvatar(url:string): Promise<WechatMiniprogram.DownloadFileSuccessCallbackResult> {
//   let requestConfig:RequestConfig={
//     url:url,
//     method:HttpRequestMethod.GET,
//   }
//   return  download(requestConfig)
// }

/**
 * 
 * @param url 文件上传地址
 * @param filePath 文件地址
 * @param fileName 文件名称
 * @param formData 
 */
export  function uploadImage(filePath:string,
                              formData:any): Promise<LengyuHttpResponse> {
  let requestConfig:RequestConfig={
    url:"work-order/upload-file",
    method:HttpRequestMethod.GET,
    token:true,
    data:formData,
  }
  return  upload(requestConfig,filePath);
}